package com.study.mike.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.study.mike.common.exception.BizException;
import com.study.mike.model.dto.QueryUserRoleDTO;
import com.study.mike.model.dto.UserRoleDTO;
import com.study.mike.model.entity.ResourceDO;
import com.study.mike.model.po.*;
import com.study.mike.result.R;
import com.study.mike.result.RUtil;
import com.study.mike.service.UserRoleService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @author KangHui
 * @Date 2022/8/5 15:49
 */
@RequestMapping("/user/role")
@RestController
public class UserRoleController {
    @Resource
    private UserRoleService userRoleService;

    /**
     * 用户授予角色列表(拥有哪些角色)
     *
     * @param po
     * @return
     */
    @PostMapping("/query")
    public R<UserRoleDTO> getUserResource(@RequestBody @Valid GetUserRolePO po) {
        List<UserRoleDTO> userRoleDTOS = userRoleService.getUserRoles(po);
        return RUtil.success(userRoleDTOS);
    }

    /**
     * 用户授予角色
     *
     * @param po
     * @return
     */
    @PostMapping("/treeInfo")
    public R<List<QueryUserRoleDTO>> treeInfo(@RequestBody @Valid GetUserRolePO po) {
        List<QueryUserRoleDTO> trees = userRoleService.treeInfo(po);
        if (CollUtil.isEmpty(trees)) {
            throw new BizException("403", "暂无系统资源权限");
        }
        return RUtil.success(trees);
    }

    /**
     * 获取资源全路径
     *
     * @param po
     * @return
     */
    @PostMapping("/getResource")
    public R<List<String>> getResource(@RequestBody @Valid GetUserRolePO po) {
        List<String> resourceNames = userRoleService.getResource(po);
        return RUtil.success(resourceNames);
    }

    /**
     * 获取资源名全路径 -倒叙
     *
     * @param po
     * @return
     */
    @PostMapping("/getResourceName")
    public R<List<String>> getResourceName(@RequestBody @Valid GetUserRolePO po) {
        List<String> resourceNames = userRoleService.getResourceName(po);
        return RUtil.success(resourceNames);
    }

    /**
     * 获取资源名全路径 -正叙
     *
     * @param po
     * @return
     */
    @PostMapping("/getResourceNameASC")
    public R<List<String>> getResourceNameASC(@RequestBody @Valid GetUserRolePO po) {
        List<String> resourceNames = userRoleService.getResourceName(po);
        return RUtil.success(resourceNames);
    }


    /**
     * 授予用户资源
     *
     * @param po
     * @return
     */
    @PostMapping("/gran/user/resources")
    public R grantUserResources(@RequestBody @Valid GrantUserResourcesPO po) {
        if (ObjectUtil.isEmpty(po.getResources())) {
            return RUtil.error("资源不能为空");
        }
        boolean flag = userRoleService.grantUserResources(po);
        if (flag) {
            return RUtil.success("用户新增资源成功");
        }
        return RUtil.error("用户新增资源失败");
    }

    /**
     * 新增资源
     *
     * @param po
     * @return
     */
    @PostMapping("/add/resource")
    public R<ResourceDO> addResource(@RequestBody @Valid AddResourcePO po) {
        ResourceDO resourceDO = userRoleService.addResource(po);
        if (StrUtil.isEmpty(resourceDO.getName())) {
            return RUtil.error("新增资源失败");
        }
        return RUtil.success(resourceDO);
    }

    /**
     * 编辑资源
     *
     * @param po
     * @return
     */
    @PostMapping("edit/resource")
    public R<ResourceDO> editResource(@RequestBody @Valid EditResourcePO po) {
        ResourceDO resourceDO = userRoleService.editResource(po);
        if (StrUtil.isEmpty(resourceDO.getName())) {
            return RUtil.error("新增资源失败");
        }
        return RUtil.success(resourceDO);
    }

    /**
     * 新增or更新资源
     *
     * @param po
     * @return
     */
    @PostMapping("save/or/update/resource")
    public R<ResourceDO> saveOrUpdateResource(@RequestBody SaveOrUpdateResourcePO po) {
        // 资源名必输验证
        if (StrUtil.isEmpty(po.getName())) {
            return RUtil.error("资源名不能为空");
        }
        return RUtil.success(userRoleService.saveOrUpdateResource(po));
    }

    /**
     * 用户授予角色
     */
    @PostMapping("/grant/user/role")
    public R grantRoleResource(@RequestBody GrantUserRolePO po) {
        userRoleService.grantUserRole(po);
        return RUtil.success("success");
    }

}
